package net.chasing.retrofit.engine;

import net.chasing.retrofit.RetrofitManager;
import net.chasing.retrofit.api.FileService;
import net.chasing.retrofit.bean.req.DownloadInfoReq;
import net.chasing.retrofit.callback.ResponseBodyCallback;
import net.chasing.retrofit.util.RxUtil;

import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.subscribers.ResourceSubscriber;
import okhttp3.ResponseBody;

public class FileEngine {
    private final FileService mService;

    public FileEngine() {
        mService = RetrofitManager.getInstance().createRetrofitService(FileService.class);
    }

    public ResourceSubscriber<ResponseBody> downloadFile(String url, ResponseBodyCallback responseBodyCallback) {
        Flowable<ResponseBody> flowable = mService.downloadFile(url);
        return RxUtil.toSubscribeResponseBodyFlowable(flowable, responseBodyCallback);
    }

    public ResourceSubscriber<ResponseBody> downloadFile(DownloadInfoReq info, ResponseBodyCallback responseBodyCallback) {
        Flowable<ResponseBody> flowable = mService.downloadFile("bytes=" + info.getReadLength() + "-", info.getUrl());
        return RxUtil.toSubscribeResponseBodyFlowable(flowable, responseBodyCallback);
    }
}